/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.iec.edp.caf.data.multilangcolumn;

import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.data.multilang.CAFMultiLanguageColumn;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;

public interface CountryEmbededRepo extends JpaRepository<CountryWithEmbeded,String> {
//    List<CountryWithEmbeded> findAllByNameCafsuffixchs(String name);
    List<CountryWithEmbeded> findAllByNameCafMlcchs(String name);

    default List<CountryWithEmbeded> findAllByName(String name){
        EntityManager em = SpringBeanUtils.getBean(EntityManager.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<CountryWithEmbeded> query = cb.createQuery(CountryWithEmbeded.class);
        Root<CountryWithEmbeded> root = query.from(CountryWithEmbeded.class);

        query.select(root)
                .where(cb.equal(root.get("name2").get(CAFMultiLanguageColumn.fieldPrefix +"chs"), "世界"));
        List<CountryWithEmbeded> data =  em.createQuery(query).getResultList();;
        return data;
    }
}
